<?php


class TeacherController extends Controller
{
  private $_uid;

  /**
   * 初始化方法，检测登陆
   */
  public function init()
  {
    $this->checkUserLogin(2);
    $this->_uid = Yii::app()->user->id;
  }

  /**
   * 教师主页
   */
  public function actionIndex()
  {
    $this->render('index');
  }


  /**
   * 课程列表
   * @param string $keywords 搜索关键字
   */
  public function actionLesson($keywords = '')
  {
    $this->iCheck();
    $data = LessonService::instance()->getList(20, false, true, $this->_uid, $keywords);
    $this->render('lesson/list', array(
        'data' => $data
    ));
  }

  /**
   * 添加/编辑课程信息
   * @param int $id
   */
  public function actionPost_lesson($id = 0)
  {
    if ($id > 0) {
      $this->checkLesson($id);
    }
    if (Yii::app()->request->isPostRequest) {
      $result = LessonService::instance()->post($this->filter($_POST), $this->_uid);
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('teacher/lesson'));
      } else {
        $this->jsMsg($this->getError($result));
      }
    } else {
      if ($id > 0) {
        //编辑
        $data = Lesson::model()->findByPk($id);
        $this->render('lesson/post', array(
            'data' => $data
        ));
      } else {
        //添加
        $this->render('lesson/post');
      }
    }
  }

  /**
   * 设置课程状态
   * @param int $state
   */
  public function actionLesson_gateway($state = 0)
  {
    echo LessonService::instance()->setState($_POST['list'], $state) ? 1 : 0;
  }

  /**
   * 文章列表
   * @param int $lesson_id 课程编号
   * @param string $keywords 搜索关键字
   */
  public function actionArticle($lesson_id = 0, $keywords = '')
  {
    $this->checkLesson($lesson_id);
    $this->iCheck();
    $data = ArticleService::instance()->getList(20, $lesson_id, false, $keywords);
    $lesson = Lesson::model()->findByPk($lesson_id);
    $this->render('article/list', array(
        'data' => $data,
        'lesson' => $lesson
    ));
  }

  /**
   * 添加/编辑文章信息
   * @param int $id
   * @param int $lesson_id
   */
  public function actionPost_article($id = 0, $lesson_id = 0)
  {
    if ($id > 0) {
      $this->checkArticle($id, $this->_uid);
    }
    if (Yii::app()->request->isPostRequest) {
      $data = $this->filter($_POST);
      $result = ArticleService::instance()->post($data, $this->_uid);
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('teacher/article', array('lesson_id' => $lesson_id)));
      } else {
        $this->jsMsg($this->getError($result));
      }
    } else {
      $lesson = Lesson::model()->findByPk($lesson_id);
      $this->initEditor();
      if ($id > 0) {
        //编辑
        $data = Article::model()->findByPk($id);
        $this->render('article/post', array(
            'data' => $data,
            'lesson' => $lesson
        ));
      } else {
        //添加
        $this->render('article/post', array(
            'lesson' => $lesson
        ));
      }
    }
  }

  /**
   * 删除文章
   */
  public function actionDelete_article()
  {
    echo ArticleService::instance()->delete($this->filter($_POST['list']))?1:0;
  }

  /**
   * 注销登录
   */
  public function actionLogout()
  {
    Yii::app()->user->logout();
    $this->redirect(Yii::app()->createUrl('site/index'));
  }


  /**
   * 题库列表
   * @param string $keywords 搜索关键字
   */
  public function actionQuestion_bank($keywords = '')
  {
    $this->iCheck();
    $data = QuestionBankService::instance()->getList(20, false, true, $this->_uid, $keywords);
    $this->render('question_bank/list', array(
        'data' => $data
    ));
  }

  /**
   * 添加/编辑题库信息
   * @param int $id
   */
  public function actionPost_question_bank($id = 0)
  {
    if ($id > 0) {
      $this->checkQuestionBank($id);
    }
    if (Yii::app()->request->isPostRequest) {
      $result = QuestionBankService::instance()->post($this->filter($_POST), $this->_uid);
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('teacher/question_bank'));
      } else {
        $this->jsMsg($this->getError($result));
      }
    } else {
      if ($id > 0) {
        //编辑
        $data = QuestionBank::model()->findByPk($id);
        $this->render('question_bank/post', array(
            'data' => $data
        ));
      } else {
        //添加
        $this->render('question_bank/post');
      }
    }
  }

  /**
   * 设置题库状态
   * @param int $state
   */
  public function actionQuestion_bank_gateway($state = 0)
  {
    echo QuestionBankService::instance()->setState($_POST['list'], $state) ? 1 : 0;
  }

  /**
   * 题库列表
   * @param int $bank_id 题库编号
   * @param string $keywords 搜索关键字
   */
  public function actionQuestion($bank_id = 0, $keywords = '')
  {
    $this->checkQuestionBank($bank_id,$this->_uid);
    $this->iCheck();
    $data = QuestionService::instance()->getList(20, $bank_id, false, $keywords);
    $bank = QuestionBank::model()->findByPk($bank_id);
    $this->render('question/list', array(
        'data' => $data,
        'bank' => $bank
    ));
  }

  /**
   * 添加/编辑题目信息
   * @param int $id
   * @param int $bank_id
   */
  public function actionPost_question($id = 0, $bank_id = 0)
  {
    if ($id > 0) {
      $this->checkQuestion($id,$this->_uid);
    }
    if (Yii::app()->request->isPostRequest) {
      $data = $this->filter($_POST);
      $result = QuestionService::instance()->post($data, $this->_uid);
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('teacher/question', array('bank_id' => $bank_id)));
      } else {
        $this->jsMsg($this->getError($result));
      }
    } else {
      $this->iCheck();
     $bank = QuestionBank::model()->findByPk($bank_id);
      $this->initEditor();
      if ($id > 0) {
        //编辑
        $data = Question::model()->findByPk($id);
        $this->render('question/post', array(
            'data' => $data,
            'bank'=>$bank
        ));
      } else {
        //添加
        $this->render('question/post', array(
            'bank' => $bank
        ));
      }
    }
  }

  /**
   * 删除题目
   */
  public function actionDelete_question()
  {
    echo QuestionService::instance()->delete($this->filter($_POST['list']))?1:0;
  }

  /**
   * 我的回答
   */
  public function actionReply()
  {
    $data = IssueService::instance()->getUserReplyList($this->_uid,true);
    $this->render('reply/list', array(
        'data' => $data
    ));
  }

  /**
   * 我的主题
   * @param string $keywords
   */
  public function actionTopic($keywords = '')
  {
    $this->iCheck();
    $data = TopicService::instance()->getTopicList(20,false,$keywords,$this->_uid);
    $this->render('topic/list', array(
        'data' => $data
    ));
  }

  /**
   * 删除主题
   */
  public function actionDelete_topic()
  {
    echo TopicService::instance()->delete($this->filter($_POST['list']))?1:0;
  }

  /**
   * 我的回复
   * @param string $keywords
   */
  public function actionThread($keywords = '')
  {
    $this->iCheck();
    $data = TopicService::instance()->getUserThreads($this->_uid,20,$keywords);
    $this->render('thread/list', array(
        'data' => $data
    ));
  }

  /**
   * 删除回复
   */
  public function actionDelete_thread()
  {
    echo TopicService::instance()->deleteThread($this->filter($_POST['list']))?1:0;
  }
} 